package com.baiytfp.hf.HFDataExchange.action.controller.view;

import com.baiytfp.hf.HFDataExchange.common.utils.PathInfo;
import com.baiytfp.hf.baiytfp.framework.base.BaseController;
import com.baiytfp.hf.baiytfp.framework.exception.ParameterException;
import com.baiytfp.hf.baiytfp.hfutils.HFConfig;
import com.baiytfp.hf.baiytfp.hfutils.HFResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/view/error")
public class ErrorVController extends BaseController implements HandlerExceptionResolver {


	//@Override
	public ModelAndView resolveException(HttpServletRequest request,
										 HttpServletResponse response, Object handler, Exception ex) {
		Map<String, Object> model = new HashMap<String, Object>();
//		model.put("ex", ex);

		StringBuffer sbr = new StringBuffer();

		//错误提示
		sbr.append(ex.toString());
		sbr.append(ex.getMessage());

		//错误信息追踪
		StackTraceElement[] stacks = ex.getStackTrace();
		List<String> stackList = new ArrayList();
		for(StackTraceElement s: stacks){
			stackList.add(s.toString());
			//sbr.append("-------"+s.getMethodName()+" : "+s + " <br />");
			sbr.append("-------" + s + " <br />");
		}
		model.put("stackTrace", sbr.toString());
		model.put("ftl_skin", /*HFConfig.getIstance().getPropConfig("ftl_skin", "default")*/"default");
		model.put("hf_version", HFConfig.getIstance().getPropConfig("hf_version", "20170524-002"));
		model.put("path", PathInfo.getAppPath(request));
		this.writeLog(ex);
		String ftlPath = PathInfo.getTemplatePath(request);
		// 根据不同错误转向不同页面
		if (ex instanceof ParameterException) {
			ex.printStackTrace();
			return new ModelAndView(ftlPath+"/view/error/errorParameter", model);
		} else {
			ex.printStackTrace();

			try {
				Map<String, Object> jsonMap = new HashMap<>();
				jsonMap.put("ex_type", ex.toString());
				jsonMap.put("ex_desc", ex.getMessage());
				jsonMap.put("stackTraceArr", stackList);
//				jsonMap.put("stackTraceStr", sbr.toString());

				//HFResponse.getIstance().returnJson(response, jsonMap);
			}catch(Exception e){
				e.printStackTrace();
			}
			//return null;
			return new ModelAndView(ftlPath+"error/error", model);
		}
	}

	@RequestMapping("error404")
	public String error404(HttpServletRequest request,
						   HttpServletResponse response, Object handler, Exception ex){

		System.out.println("404错误 : " + ex.getMessage());

		return "error/404";
	}

	@RequestMapping("error500")
	public String error500(HttpServletRequest request,
						   HttpServletResponse response, Object handler, Exception ex){

		System.out.println("500错误 : " + ex.getMessage());

		return "error/500";
	}

	//TODO 写入日志,还没开发
	private void writeLog(Exception ex){

		//ex.printStackTrace();
	}


}
